package com.funtonow.jqdk.upload;

import android.content.Context;

import com.tencent.cos.xml.CosXmlService;
import com.tencent.cos.xml.CosXmlServiceConfig;
import com.tencent.cos.xml.exception.CosXmlClientException;
import com.tencent.cos.xml.exception.CosXmlServiceException;
import com.tencent.cos.xml.listener.CosXmlProgressListener;
import com.tencent.cos.xml.listener.CosXmlResultListener;
import com.tencent.cos.xml.model.CosXmlRequest;
import com.tencent.cos.xml.model.CosXmlResult;
import com.tencent.cos.xml.model.object.PutObjectRequest;
import com.tencent.qcloud.core.auth.QCloudCredentialProvider;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author Jiajien
 * @date 2019/5/6
 */
public class UploadFileUtils {

    private static UploadFileUtils instance = null;

    private final String appid = "1258167336";
    private final String region = "ap-guangzhou";

    private final String URL_HEAD = "https://tkjy-1258167336.file.myqcloud.com";

    private final String COS_URL = "https://tkjy-1258167336.cos.ap-guangzhou.myqcloud.com";

    public static final String HEAD_IMAGE = "hlyqs/headimage/";
    public static final String ALBUM = "album/";
    public static final String BROADCAST = "broadcast/";

    private UploadListener uploadListener;
    private UploadVideoListener uploadVideoListener;

    public static synchronized UploadFileUtils getInstance() {
        if (instance == null) {
            instance = new UploadFileUtils();
        }
        return instance;
    }

    public void uploadFile(Context context, String cosPath, String srcPath) {

        //创建 CosXmlServiceConfig 对象，根据需要修改默认的配置参数
        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .isHttps(true)
                .setDebuggable(true)
                .builder();
        URL url = null;
        try {
            url = new URL("https://tkjy-1258167336.cos.ap-guangzhou.myqcloud.com");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        // 初始化 QCloudCredentialProvider   提供临时密钥
        assert url != null;
        QCloudCredentialProvider credentialProvider = new MyCredentialProvider();
        CosXmlService cosXmlService = new CosXmlService(context, serviceConfig, credentialProvider);
        String bucket = "tkjy-1258167336";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);

        putObjectRequest.setProgressListener(new CosXmlProgressListener() {
            @Override
            public void onProgress(long progress, long max) {
            }
        });

        // 使用异步回调上传
        cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult cosXmlResult) {
                // todo Put object success...
                String name = cosXmlResult.accessUrl.split(COS_URL)[cosXmlResult.accessUrl.split(COS_URL).length - 1];
                uploadListener.uploadSuccess(URL_HEAD + name);
            }

            @Override
            public void onFail(CosXmlRequest cosXmlRequest, CosXmlClientException clientException, CosXmlServiceException serviceException) {
                uploadListener.uploadFail();
            }
        });

    }

    public void uploadFile(Context context, String cosPath, byte[] srcPath) {

        //创建 CosXmlServiceConfig 对象，根据需要修改默认的配置参数
        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .isHttps(true)
                .setDebuggable(true)
                .builder();
        URL url = null;
        try {
            url = new URL("https://tkjy-1258167336.cos.ap-guangzhou.myqcloud.com");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        // 初始化 QCloudCredentialProvider   提供临时密钥
        assert url != null;
        QCloudCredentialProvider credentialProvider = new MyCredentialProvider();
        CosXmlService cosXmlService = new CosXmlService(context, serviceConfig, credentialProvider);
        String bucket = "tkjy-1258167336";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);
        putObjectRequest.setProgressListener(new CosXmlProgressListener() {
            @Override
            public void onProgress(long progress, long max) {
            }
        });

        // 使用异步回调上传
        cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult cosXmlResult) {
                // todo Put object success...
                String name = cosXmlResult.accessUrl.split(COS_URL)[cosXmlResult.accessUrl.split(COS_URL).length - 1];
                uploadListener.uploadSuccess(URL_HEAD + name);
            }

            @Override
            public void onFail(CosXmlRequest cosXmlRequest, CosXmlClientException clientException, CosXmlServiceException serviceException) {
                uploadListener.uploadFail();
            }
        });

    }

    public void uploadVideoFile(Context context, String cosPath, String srcPath) {

        //创建 CosXmlServiceConfig 对象，根据需要修改默认的配置参数
        CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
                .setRegion(region)
                .isHttps(true)
                .setDebuggable(true)
                .builder();
        URL url = null;
        try {
            url = new URL("https://tkjy-1258167336.cos.ap-guangzhou.myqcloud.com");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        // 初始化 QCloudCredentialProvider   提供临时密钥
        assert url != null;
        QCloudCredentialProvider credentialProvider = new MyCredentialProvider();
        CosXmlService cosXmlService = new CosXmlService(context, serviceConfig, credentialProvider);
        String bucket = "tkjy-1258167336";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, cosPath, srcPath);

        putObjectRequest.setProgressListener(new CosXmlProgressListener() {
            @Override
            public void onProgress(long progress, long max) {
            }
        });

        // 使用异步回调上传
        cosXmlService.putObjectAsync(putObjectRequest, new CosXmlResultListener() {
            @Override
            public void onSuccess(CosXmlRequest cosXmlRequest, CosXmlResult cosXmlResult) {
                // todo Put object success...
                String name = cosXmlResult.accessUrl.split(COS_URL)[cosXmlResult.accessUrl.split(COS_URL).length - 1];
                uploadVideoListener.uploadVideoSuccess(URL_HEAD + name);
            }

            @Override
            public void onFail(CosXmlRequest cosXmlRequest, CosXmlClientException clientException, CosXmlServiceException serviceException) {
                uploadVideoListener.uploadVideoFail();
            }
        });

    }


    public interface UploadListener {

        /**
         * 上传成功
         *
         * @param result
         */
        void uploadSuccess(String result);

        /**
         * 上传失败
         */
        void uploadFail();

    }


    public interface UploadVideoListener {

        /**
         * 上传成功
         *
         * @param result
         */
        void uploadVideoSuccess(String result);

        /**
         * 上传失败
         */
        void uploadVideoFail();

    }

    public void setUploadListener(UploadListener uploadListener) {
        this.uploadListener = uploadListener;
    }

    public void setUploadVideoListener(UploadVideoListener uploadVideoListener) {
        this.uploadVideoListener = uploadVideoListener;
    }

}
